package Client.GUI;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.ListSelectionModel;
import Client.logic.ClientIF;
import Server.DataBase.Teacher;
import Server.GUI.MyJTable;
import Server.Message.MessageGetTUnitReplay;
import Server.Message.MessageLessonReplay;
import Server.Message.MessageTeacherLesson;
import Server.Message.MessageTeacherTUnit;

public class TeacherInfoPanel extends MyJPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private MyJTable lessonTable, tUnitTable;
	private Teacher user;
	private JLabel labelMaxV, labelCurrent;
	
	public TeacherInfoPanel(ClientIF client, Teacher user) {
		super(PanelType.PERSONAL_FILE_PANEL, client);
		setLayout(null);
		
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"teacher info.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(152, 11, 491, 78);
			add(lblAddClass);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		this.user = user;
		init();
	}
	
	private void initLabels(){
		JLabel lblLesson = new JLabel("Lesson:");
		lblLesson.setBounds(31, 123, 63, 14);
		add(lblLesson);
		
		JLabel lblTunit = new JLabel("Tunit:");
		lblTunit.setBounds(572, 123, 46, 14);
		add(lblTunit);
		
		JLabel lblMaxHours = new JLabel("Max hours:");
		lblMaxHours.setBounds(572, 291, 90, 14);
		add(lblMaxHours);
		
		labelMaxV = new JLabel("");
		labelMaxV.setBounds(679, 291, 46, 14);
		add(labelMaxV);
		
		JLabel lblCurrentHours = new JLabel("Current hours:");
		lblCurrentHours.setBounds(572, 334, 111, 14);
		add(lblCurrentHours);
		
		labelCurrent = new JLabel("");
		labelCurrent.setBounds(679, 334, 46, 14);
		add(labelCurrent);
	}
	
	private void initComboBox(){
	}
	
	private void initTables(){
		lessonTable = new MyJTable();
		lessonTable.setBounds(87, 123, 452, 312);
		lessonTable.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		add(lessonTable);
		
		tUnitTable = new MyJTable();
		tUnitTable.setBounds(616, 123, 215, 128);
		tUnitTable.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		add(tUnitTable);
		
		
	}
	
	private void initBtn(){
	}
	
	public void init(){
		initComboBox();
		initLabels();
		initTables();
		initBtn();
		
		labelMaxV.setText(Integer.toString(user.getMaxHours()));
		labelCurrent.setText(Integer.toString(user.getHours()));
		getClient().sendMsgToServer(new MessageTeacherLesson(user.getIdUser()));
		MessageLessonReplay msg = (MessageLessonReplay) getClient().getMessageFromServer();
		
		LessonTableModel ltm = new LessonTableModel();
		ltm.setArray(msg.getArray());
		lessonTable.setModel(ltm);
		
		getClient().sendMsgToServer(new MessageTeacherTUnit(user.getIdUser()));
		MessageGetTUnitReplay msg2 = (MessageGetTUnitReplay) getClient().getMessageFromServer();
		
		TUnitTableModel ttm = new TUnitTableModel();
		ttm.setArray(msg2.getArray());
		tUnitTable.setModel(ttm);
	}
	
	
	
	@Override
	public MyJPanel pushPanel() {
		return new TeacherInfoPanel(getClient(), user);
	}
}
